package main

import (
	"fmt"
	"log"
	"sync"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
)

var (
	tracer *go2sky.Tracer
	mu     sync.Mutex
)

func getTracer(service string) *go2sky.Tracer {
	if tracer != nil {
		return tracer
	}
	mu.Lock()
	defer mu.Unlock()
	if tracer != nil {
		return tracer
	}
	fmt.Println("get Tracer")

	re, err := reporter.NewGRPCReporter("localhost:11800")
	if err != nil {
		log.Fatalf("new reporter error %v \n", err)
	}

	tracerTemp, err := go2sky.NewTracer(service, go2sky.WithReporter(re), go2sky.WithInstance(IPV4()))
	if err != nil {
		log.Fatalf("create tracer error %v \n", err)
	}

	tracer = tracerTemp
	return tracer
}
